package com.example.controller;

import com.example.service.DeptHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author: 21050134
 * @date: 2023/10/8 14:16
 * @description:
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "dept_Global_FallbackMethod")
public class HystrixController_Consumer {

	@Resource
	private DeptHystrixService deptHystrixService;

	@RequestMapping(value = "/consumer/dept/hystrix/ok/{id}")
	public String deptInfo_Ok(@PathVariable("id") Integer id){
		String s = deptHystrixService.deptInfo_Ok(id);
		log.info(s);
		return s;
	}


	@RequestMapping(value = "/consumer/dept/hystrix/timeout/{id}")
	@HystrixCommand(fallbackMethod = "dept_TimeoutHandler")
	public String deptInfo_Timeout(@PathVariable("id") Integer id){
		String s = deptHystrixService.deptInfo_Timeout(id);
		log.info(s);
		return s;
	}

	public String dept_TimeoutHandler(Integer id){
		log.info("deptInfo_Timeout 出错，服务已被降级！");
		return "服务端系统繁忙，请稍后再试！（客户端 deptInfo_Timeout 专属的回退方法触发）";
	}


	/**
	 * 全局的fallback
	 */

	public String dept_Global_FallbackMethod(){
		log.info("deptInfo_Timeout 出错，服务已被降级，客户端 全局  回退方法触发！");
		return "服务端系统繁忙，请稍后再试！（客户端 全局  回退方法触发）";
	}

	@RequestMapping(value = "/consumer/dept/hystrix/timeoutV2/{id}")
	@HystrixCommand
	public String deptInfo_TimeoutV2(@PathVariable("id") Integer id){
		String s = deptHystrixService.deptInfo_Timeout(id);
		log.info(s);
		return s;
	}

}
